const query=require("../db/index")
const {Errs, success}=require("../moddle/index")

class List{
    getlist=async(ctx)=>{
        let sql=`select * from ku`
        const data=await query(sql)
        ctx.body=data
    }
    putlist=async(ctx)=>{
        let sql =`select * from ku where 1=1 `
        const {keyword,pageIndex,pageSize,id}=ctx.request.body
         if(keyword){
            sql +=`and usename like "%${keyword}%"`
         }
         if(id){
              sql +=`and id = "${id}"`
        } 
        if(pageIndex&&pageSize){
              sql +=`limit ${(pageIndex-1)*pageSize},${pageSize}`
        }
        const data=await query(sql)
        ctx.body=success({
            data:data
        })
    }
    carlist=async(ctx)=>{
        let sql=`select * from kus`
        const data=await query(sql)
        ctx.body=data 
    }
    delelist=async(ctx)=>{
       const {id}=ctx.params
       let sql=`delete from kus where id=${id}`
       const data=await query(sql)
       if(data.affectedRows === 1){
        ctx.body = "成功"
    } else {
        ctx.body = error('失败')
    }
    }
    add=async(ctx)=>{
       const {id,usename,password}=ctx.request.body
       let sql=`select * from kus where id="${id}"`
       const data=await query(sql)
       let sqls=''
        if(data.length){
          const datas=data[0]
         sqls =`update kus set count="${datas.count+=1}" where id="${datas.id}"`
          
       }else{ 
           console.log(id,usename,password)
        sqls =`insert into kus(password,usename,id,count)values("${password}","${usename}","${id}",1)` }
       const datass =await query(sqls)
       if(datass.affectedRows === 1){
           ctx.body = "成功"
       } else {
           ctx.body = error('添加失败')
       }
    }
    
}
module.exports=new List()